<?php
class Admin_Model_Translation
{    
    private static $_instance;
    
    private function __construct() {
    }
    
    /**
     * @return Admin_Model_Translation
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    /**
     * Insert translation
     * @param array $data
     * @return 1 on success, 0 on failure
     * */
    public function insert($data) {
        $dao = Admin_Model_DAO_Translation::factory();
        if(!$this->isValid($data)) {
            return 0;
        }
        try {
            $result = $dao->insert($data);
            if($result) {//Log
            	$data['id'] = $result;
            	Utils_Global::storeBackendLog('insert', Admin_Model_DAO_Translation::$_table, $result, $data);
            }
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }
    
    public function update($id, $data) {
        $dao = Admin_Model_DAO_Translation::factory();
        try {
        	$result = $dao->update($id, $data);
        	if($result) {//Log
        		Utils_Global::storeBackendLog('update', Admin_Model_DAO_Translation::$_table, $result, $data);
        	}
        } catch (Exception $exc) {
        	prBinh($exc);
        	Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }   

    public function updateI18n($i18n, $lang, $data) {
        $dao = Admin_Model_DAO_Translation::factory();
        try {
        	$result = $dao->updateI18n($i18n, $lang, $data);
        	if($result) {//Log
        		Utils_Global::storeBackendLog('update', Admin_Model_DAO_Translation::$_table, $result, $data);
        	}
        } catch (Exception $exc) {
        	prBinh($exc);
        	Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }
    
    /**
     * Select translation
     * @param array $options
     * @return 1 on success, 0 on failure
     * */
    public function select($options = array()) {
        $result = array();
        $dao = Admin_Model_DAO_Translation::factory();
        try {
            $result = $dao->select($options);
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }
    
    public function isValid($data) {
        if(!$data['i18n_id'] ||
            !$data['content'] || !$data['lang']) {
            return false;
        }
        
        return true;
    }
}